package com.example.fresheveryday.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.fresheveryday.po.Blog;
import com.example.fresheveryday.restful.JsonResult;
import com.example.fresheveryday.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/blog")
public class BlogController {
  @Autowired
  private BlogService blogService;
  //获取表单内容
  @RequestMapping("/getBlogList")
  public IPage<Blog> getBlog(@RequestBody Map map){
    Page<Blog> page =new Page<>();
    page.setCurrent(Integer.valueOf(map.get("pageNo").toString()))
      .setTotal(blogService.count())
      .setSize(Integer.valueOf(map.get("pageSize").toString()));
    return  blogService.getAllBlog(page);
  }
  @RequestMapping("/getAll")
  public List<Blog> getAllBlog(){
    return blogService.getThree();
  }
  @RequestMapping("/getSix")
  public List<Blog> getSix(){
    return blogService.getSix();
  }
  @RequestMapping("/getOneBlog")
  public Blog getOneBlog(int blogId){
    System.out.println(blogId);
    QueryWrapper wrapper = new QueryWrapper();
    wrapper.eq("blogId",blogId);
    System.out.println("blogId:" + blogId);
    System.out.println(blogService.getOne(wrapper));
    return blogService.getOne(wrapper);
  }
  @RequestMapping("/saveOrUpdate")
  public JsonResult saveOrUpdate(@RequestBody Blog blog){
    System.out.println("修改"+blog);
    blogService.saveOrUpdate(blog);
    return JsonResult.success("success");
  }
  //通过标题获取博客数据
  @RequestMapping("/getBlogByTitle")
  public IPage<Blog> getBlogByTitle(@RequestBody Map map){
    Page<Blog> page=new Page<>();
    String title=map.get("title").toString();
    QueryWrapper wrapper=new QueryWrapper();
    wrapper.eq("title",title);
    page.setCurrent(Integer.valueOf(map.get("pageNo").toString()))
      .setTotal(blogService.countByConditions(wrapper))
      .setSize(Integer.valueOf(map.get("pageSize").toString()));
    return blogService.getBlogByConditions(page,wrapper);
  }

  //通过ID获取博客数据
  @RequestMapping("/getBlogById")
  public Blog getBlogById(int blogId){
    System.out.println(blogService.getById(blogId));
    return blogService.getById(blogId);
  }
  //删除
  @RequestMapping("/delete")
  public JsonResult del(int blogId) {
    blogService.removeById(blogId);
    return JsonResult.success("success");
  }

}
